package io.itit.ecp.admin.provide.task;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import io.itit.ecp.admin.server.entity.CarbonEmissionFactorEntity;
import io.itit.ecp.admin.server.entity.CarbonSinkFactorEntity;
import io.itit.ecp.admin.server.entity.EmissionReductionFactorEntity;
import io.itit.ecp.admin.server.service.ICarbonEmissionFactorService;
import io.itit.ecp.admin.server.service.ICarbonSinkFactorService;
import io.itit.ecp.admin.server.service.IEmissionReductionFactorService;
import io.itit.grass.scheduler.server.entity.TaskSchedulerEntity;
import io.itit.grass.scheduler.server.task.IGrassTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/*
 **********************
 * @Author tangdongming
 * @ClassName EmissionFactorTask
 * @Description 因子定时任务
 * @Date 2023/11/30 9:59
 **********************
 **/
@Slf4j
@Component
public class EmissionFactorTask implements IGrassTask {

    @Resource
    private ICarbonEmissionFactorService carbonEmissionFactorService;

    @Resource
    private ICarbonSinkFactorService carbonSinkFactorService;

    @Resource
    private IEmissionReductionFactorService emissionReductionFactorService;

    @Override
    public String run(TaskSchedulerEntity taskScheduler) {
        DateTime date = DateUtil.date();
        DateTime nextDate = DateUtil.beginOfMonth(DateUtil.offset(date, DateField.MONTH, 1));
        // 执行碳排放因子任务
        executeCarbonEmissionFactor(date, nextDate);
        // 执行碳汇因子任务
        executeCarbonSinkFactor(date, nextDate);
        // 执行碳减因子任务
        executeEmissionReductionFactor(date, nextDate);
        return "因子定时任务执行完成！";
    }

    private void executeCarbonEmissionFactor(DateTime date, DateTime nextDate) {
        long count = carbonEmissionFactorService.lambdaQuery().eq(CarbonEmissionFactorEntity::getSolidifyDate, nextDate).count();
        if (count != 0) {
            log.error("碳排放因子数据已存在！日期：{}", nextDate);
            return;
        }
        List<CarbonEmissionFactorEntity> resultList = carbonEmissionFactorService.lambdaQuery().eq(CarbonEmissionFactorEntity::getSolidifyDate, DateUtil.beginOfMonth(date)).list();
        resultList.forEach((x) -> {
            x.setId(null);
            x.setCreateTm(date);
            x.setModifyTm(date);
            x.setSolidifyDate(nextDate);
        });
        carbonEmissionFactorService.saveBatch(resultList);
        log.info("碳排放因子数据生成成功！日期：{}", nextDate);
    }

    private void executeCarbonSinkFactor(DateTime date, DateTime nextDate) {
        long count = carbonSinkFactorService.lambdaQuery().eq(CarbonSinkFactorEntity::getSolidifyDate, nextDate).count();
        if (count != 0) {
            log.error("碳汇因子数据已存在！日期：{}", nextDate);
            return;
        }
        List<CarbonSinkFactorEntity> resultList = carbonSinkFactorService.lambdaQuery().eq(CarbonSinkFactorEntity::getSolidifyDate, DateUtil.beginOfMonth(date)).list();
        resultList.forEach((x) -> {
            x.setId(null);
            x.setCreateTm(date);
            x.setModifyTm(date);
            x.setSolidifyDate(nextDate);
        });
        carbonSinkFactorService.saveBatch(resultList);
        log.info("碳汇因子数据生成成功！日期：{}", nextDate);
    }

    private void executeEmissionReductionFactor(DateTime date, DateTime nextDate) {
        long count = emissionReductionFactorService.lambdaQuery().eq(EmissionReductionFactorEntity::getSolidifyDate, nextDate).count();
        if (count != 0) {
            log.error("碳减因子数据已存在！日期：{}", nextDate);
            return;
        }
        List<EmissionReductionFactorEntity> resultList = emissionReductionFactorService.lambdaQuery().eq(EmissionReductionFactorEntity::getSolidifyDate, DateUtil.beginOfMonth(date)).list();
        resultList.forEach((x) -> {
            x.setId(null);
            x.setCreateTm(date);
            x.setModifyTm(date);
            x.setSolidifyDate(nextDate);
        });
        emissionReductionFactorService.saveBatch(resultList);
        log.info("碳减因子数据生成成功！日期：{}", nextDate);
    }

}
